<?php

namespace App\Http\Controllers\V1\BasicData;

use App\Models\V1\ApplyPays;
use App\Models\V1\NewStudent;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Log;

class PayCollectController extends Controller
{
    /**
     * 显示支付汇总数据管理页面
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('v1.collect.pays');
    }

    /**
     * 获取支付汇总数据
     *
     * @param Request $request
     */
    public function postIndex(Request $request)
    {
        $response = [
            'code' => 1,
            'msg' => '',
            'count' => 0,
            'data' => [],
        ];
        // 获取符合查询条件的班级数据
        $currentPage = $request->get('page');
        $rows = $request->get('limit');
        $keywords = $request->get('keywords');

        $reqParams = $request->all();
        $year = isset($reqParams['year']) ? $reqParams['year'] : '';

        // 获取符合查询条件的班级数据
        $data = $this->getPaysCollectList($currentPage, $rows, $keywords, $year);
        $formatList = array();

        if (!$data->isEmpty()) {
            $count = $data->total() > 0 ? $data->total() : 0;
            if ($count > 0) {
                // 格式化数据
                foreach ($data->items() as $item) {
                    $exam_code = $item['exam_code'];
                    $formatItem['exam_code'] = $exam_code;
                    $student = $this->getNewStudentByCode($exam_code);
                    $formatItem['student_name'] = $student['name'];
                    $formatItem['sex'] = $this->formatStudentSex($student['sex']);
                    $formatItem['card_number'] = $student['card_number'];
                    $formatItem['enrollment_directory'] = $student['enrollment_directory'];

//                    $amount = $this->getPayAmountByCode($exam_code);

                    $amount = $item['amount'];
                    $formatItem['amount'] = $amount;
                    $formatItem['status'] = '已缴费';
                    $formatItem['type'] = $this->formatPayType($item['type']);
                    $formatItem['updated_at'] = $item['updated_at']->format('Y-m-d H:i:s');
                    $formatItem['created_at'] = $item['created_at']->format('Y-m-d H:i:s');
                    array_push($formatList, $formatItem);
                }
                $response['msg'] = '加载数据成功';
            }
        } else {
            $response['msg'] = '暂无数据';
            $count = 0;
            $formatList = [];
        }
        $response['code'] = 0;
        $response['count'] = $count;
        $response['data'] = $formatList;
        return $response;
    }


    ///////////////////////////////////////////////////////////////////
    /// 获取数据功能函数
    ///////////////////////////////////////////////////////////////////
    /**
     * 分页查询符合条件的迎新列表数据
     *
     * @param int $page 当前页数
     * @param $rows 每页显示条数
     * @param $keywords 搜索关键词
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator 分页对象
     */
    private function getPaysCollectList($page = 1, $rows, $keywords, $year)
    {
        if ($year != '') {
            //不存在根据指定字段排序
            if (!isset($keywords) || empty($keywords)) {
                $data = ApplyPays::where('status', true)->orderBy('created_at', 'desc')
                    ->where('created_at', 'like', '%' . $year . '%')
                    ->paginate($rows, ['*'], 'page', $page);
            } else {
                $data = ApplyPays::where('status', true)->where('exam_code', 'like', '%' . $keywords . '%')->orderBy('created_at', 'desc')
                    ->where('created_at', 'like', '%' . $year . '%')
                    ->paginate($rows, ['*'], 'page', $page);
            }
        } else {
            //不存在根据指定字段排序
            if (!isset($keywords) || empty($keywords)) {
                $data = ApplyPays::where('status', true)->orderBy('created_at', 'desc')->paginate($rows, ['*'], 'page', $page);
            } else {
                $data = ApplyPays::where('status', true)->where('exam_code', 'like', '%' . $keywords . '%')->orderBy('created_at', 'desc')
                    ->paginate($rows, ['*'], 'page', $page);
            }
        }
        return $data;

    }

    /***
     * 根据学生考试号获取学生详细信息
     * @param $code
     * @return mixed
     */
    private function getNewStudentByCode($code)
    {
        $student = NewStudent::where('exam_code', $code)->first();
        return $student;
    }

    /***
     * 根据学生考试号获取学生支付金额
     * @param $code
     * @return mixed
     */
//    private function getPayAmountByCode($code)
//    {
//        $amount = ApplyPays::where('exam_code', $code)->sum('amount');
//        return $amount;
//    }

    /***
     * 检测是否缴费
     * @param $code
     * @return mixed
     */
    private function formatStudentSex($val)
    {
        if ($val == 1) {
            return '男';
        } elseif ($val == 2) {
            return '女';
        } else {
            return '';
        }
    }

    /***
     * 学生缴费方式
     * @param $code
     * @return mixed
     */
    private function formatPayType($val)
    {
        if ($val == 1) {
            return '支付宝';
        } elseif ($val == 2) {
            return '微信';
        } elseif ($val == 3) {
            return '现场缴费';
        } else {
            return ' ';
        }
    }

}
